#ifndef WALKAWARE_SERVICES_POLLINGSERVICE_H
#define WALKAWARE_SERVICES_POLLINGSERVICE_H

#include <QMutex>
#include <QWaitCondition>

#include <WalkaWare/Services/Detail/RunnableService.h>
#include <WalkaWare/Services/Detail/ServiceThread.h>

namespace WalkaWare
{

namespace Services
{

class PollingService : public Detail::RunnableService
{
	Q_OBJECT

	public:

		typedef Detail::RunnableService BaseType;
		typedef PollingService          ThisType;

		PollingService(void)
			: end(false)
		{
			this->sThread = new Detail::ServiceThread(this);

			this->connect(this->sThread,     SIGNAL(started()),  SIGNAL(started()));
			this->connect(this->sThread,     SIGNAL(finished()), SIGNAL(finished()));
		}

	public slots:

		virtual void start(void)
		{
			this->quit();
			this->end = false;
			this->sThread->start();
		}

		virtual void quit(void)
		{
			if (!this->isRunning()) return;

			this->end = true;
			this->sCondition.wakeAll();

			this->sThread->quit();
			this->sThread->wait();
			this->end = false;
		}

	protected:

		QMutex         sMutex;
		QWaitCondition sCondition;
		bool           end;
};

} // end namespace Services

} // end namespace WalkaWare

#endif // WALKAWARE_SERVICES_SIGNALEDSERVICE_H
